"""
    Author:
    Date: 2019-01-07

    微信管理后台-公会管理页面
"""

from django.shortcuts import render

from .. import helpers
from .. helpers import gma_login_required
from entities import guild_mgr
from utils import util


@gma_login_required
def index(request):
    """ 公会首页 """
    guilds = guild_mgr.get_guild_list(request=request)

    context = helpers.build_context(request)
    context['total_guild_count'] = len(guilds)
    return render(request, 'gma/guild/guild_home.html', context)


@gma_login_required
def guild_list(request):
    """ 公会列表 """
    guilds = guild_mgr.get_guild_list(request=request)

    context = helpers.build_context(request)
    context['guilds'] = guilds
    return render(request, 'gma/guild/guild_list.html', context)


@gma_login_required
def guild_detail(request, guild_id):
    """ 公会详情 """
    guild = guild_mgr.get_guild_info(guild_id)

    # 判断用户是否有权限管理指定的公会
    if not helpers.can_access_guild(request, guild_id):
        return helpers.show_page_tips(request, '权限不足!')

    context = helpers.build_context(request)
    context['guild'] = guild
    context['total_members_count'] = len(guild_mgr.get_guild_members(guild_id))
    return render(request, 'gma/guild/guild_detail.html', context)


@gma_login_required
def guild_members(request, guild_id):
    """ 公会成员列表 """
    guild = guild_mgr.get_guild_info(guild_id)

    context = helpers.build_context(request)
    context['guild'] = guild
    context['members'] = guild_mgr.get_guild_members(guild_id)
    return render(request, 'gma/guild/guild_member_list.html', context)


@gma_login_required
def create_guild(request):
    """ 创建公会 """
    context = helpers.build_context(request)
    return render(request, 'gma/guild/create_guild.html', context)


@gma_login_required
def guild_recruit(request, guild_id):
    """ 公会招募成员 """
    guild = guild_mgr.get_guild_info(guild_id)

    context = helpers.build_context(request)
    context['guild'] = guild
    return render(request, 'gma/guild/guild_recruit.html', context)


@gma_login_required
def bind_guild_mobile(request, guild_id):
    """ 绑定手机号码 """
    guild = guild_mgr.get_guild_info(guild_id)

    context = helpers.build_context(request)
    context['guild'] = guild
    return render(request, 'gma/guild/bind_guild_mobile.html', context)


@gma_login_required
def daily_stat(request, guild_id):
    """ 每日统计 """
    guild = guild_mgr.get_guild_info(guild_id)
    if guild is None:
        return helpers.show_page_tips(request, f'无效的公会id: {guild_id}')

    context = helpers.build_context(request)
    context['guild'] = guild
    context['stat_data'] = guild_mgr.get_guild_daily_stat_data(guild_id, 10)
    return render(request, 'gma/guild/guild_stat.html', context)


@gma_login_required
def weekly_stat(request, guild_id):
    """ 每周统计 """
    guild = guild_mgr.get_guild_info(guild_id)
    if guild is None:
        return helpers.show_page_tips(request, f'无效的公会id: {guild_id}')

    context = helpers.build_context(request)
    context['guild'] = guild
    context['stat_data'] = guild_mgr.get_guild_weekly_stat_data(guild_id, 12)
    return render(request, 'gma/guild/guild_stat.html', context)


@gma_login_required
def monthly_stat(request, guild_id):
    """ 每月统计 """
    guild = guild_mgr.get_guild_info(guild_id)
    if guild is None:
        return helpers.show_page_tips(request, f'无效的公会id: {guild_id}')

    context = helpers.build_context(request)
    context['guild'] = guild
    context['stat_data'] = guild_mgr.get_guild_monthly_stat_data(guild_id, 6)
    return render(request, 'gma/guild/guild_stat.html', context)


@gma_login_required
def user_stat(request, guild_id, begin, end):
    """ 获取指定时间范围内的统计数据 """
    guild = guild_mgr.get_guild_info(guild_id)
    if guild is None:
        return helpers.show_page_tips(request, f'无效的公会id: {guild_id}')

    context = helpers.build_context(request)
    context['template'] = 'guild/user_list.html'
    context['guild'] = guild
    context['begin_time'] = begin[:10]
    context['end_time'] = end[:10]
    context['users'] = guild_mgr.get_guild_members_stat_data(guild['id'], begin, end)
    return render(request, 'gma/guild/guild_user_stat.html', context)


@gma_login_required
def archive_list(request, guild_id):
    """ 结算列表 """
    guild = guild_mgr.get_guild_info(guild_id)
    if guild is None:
        return helpers.show_page_tips(request, f'无效的公会id: {guild_id}')

    context = helpers.build_context(request)
    context['template'] = 'guild/archive_list.html'
    context['guild'] = guild
    context['archive_data'] = guild_mgr.get_guild_archive_data(guild['id'])
    return render(request, 'gma/guild/guild_archive_list.html', context)


@gma_login_required
def archive_detail(request, guild_id, batch):
    """ 结算详情 """
    guild = guild_mgr.get_guild_info(guild_id)
    if guild is None:
        return helpers.show_page_tips(request, f'无效的公会id: {guild_id}')

    context = helpers.build_context(request)
    context['template'] = 'guild/archive_list.html'
    context['guild'] = guild
    context['batch'] = batch
    context['members'] = guild_mgr.get_guild_archive_detail(guild['id'], batch)
    return render(request, 'gma/guild/guild_archive_detail.html', context)


@gma_login_required
def download_guild_archive_data(request, guild_id, batch):
    """ 下载结算详情文件 """
    file_path = guild_mgr.fetch_guild_archive_file(guild_id, batch)
    return util.download_file(file_path)


if __name__ == '__main__':
    print('ok')

